package biznussoft.metercor.fragments;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import biznussoft.metercor.R;
import biznussoft.metercor.adapter.ActivitesAdapter;
import biznussoft.metercor.entity.ActivityModel;
import biznussoft.metercor.entity.ServiceOrderModel;
import biznussoft.metercor.listener.Globallistener;


public class ServiceOrderActivityRecordsFragment extends Fragment implements OnItemClickListener {
	private ServiceOrderModel mobel;
	static ArrayList<ActivityModel> list ;          
	private ListView lv;
	private Bundle args;
	private Globallistener listener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.service_order_activity_records, null);
	}


	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		listener = (Globallistener) getActivity();         
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		lv = (ListView) getView().findViewById(R.id.listview);

		try{
			args = getArguments();
			if (args != null) {
				mobel = ServiceOrderRecordsFragment.serviceOrderList.get(args.getInt("Position"));
				list = ServiceOrderRecordsFragment.activityDetailsList.get(mobel.getId());
				Log.i("NCS","Size is  :"+list.size());
				

				Collections.sort(list, new ListComparator());
				ActivitesAdapter adapter = new ActivitesAdapter(getActivity(),list);
				lv.setAdapter(adapter);
			}
		}catch(Exception ex){

		}


		lv.setOnItemClickListener(this);



	}



	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

		listener.getActivityResponse(position);
	}
	
	class ListComparator implements Comparator<ActivityModel>,Serializable
	{
	public int compare(ActivityModel o1, ActivityModel o2)
	{
	Log.i("Siva","entered into day comparator");

	return o1.getStep().compareTo(o2.getStep());

	}

	}

}
